#include "stdafx.h"
#include "Pronunciation.h"


Pronunciation::Pronunciation()
{
	//InitPronunciationLib();
}

Pronunciation::~Pronunciation()
{
}

bool Pronunciation::MatchPronunciation( CString* str1,CString* str2 )
{
	if (str1->GetLength() != str2->GetLength())
	{
		return false;
	}
	for (int i=0;i<str1->GetLength();i++)
	{
		if (GetPronunciation((CString)(str1->GetAt(i))) != GetPronunciation((CString)(str2->GetAt(i))) || 
			wcscmp(GetPronunciation((CString)(str1->GetAt(i))),_T("")) == 0)
		{
			return false;
		}
	}
	return true;
}

CString Pronunciation::GetPronunciation(CString str)
{
	std::map<int , CString*>::iterator it = m_PronunciationLib.find(str[0]);
	if (it != m_PronunciationLib.end())
	{
		return *(it->second);
	}
	return _T("");
}
std::map<CString* , CString*>::iterator Pronunciation::GetIterator(CString* index, std::map<CString*,CString*> mpContainer)
{
	std::map<CString* , CString*>::iterator it = mpContainer.begin();
	while (it != mpContainer.end())
	{
		if (wcscmp(index->GetString(),(it->first)->GetString()) == 0)
		{
			return it;
		}
		it++;
	}
	return it;
}

void Pronunciation::InitPronunciationLib()
{
	CString strTemp;
	CFile file;
	CFileException e;
	if(!file.Open(_T("e:\\pronunciation.txt"),CFile::modeRead,&e))
	{
		AfxMessageBox(_T("file don't exist"));
	}
	wchar_t pBuf[0x3FFFF];
	file.Read(pBuf,0x3FFFF);
	strTemp.SetString(pBuf);
	int index = 0;
	int iPos = 0;
	while ( iPos != -1 )
	{
		iPos = strTemp.Find(_T("\t"));
		CString strSimpleWord;
		CString* strPronunciation = new CString();
		CString strLeft;
		strLeft = (iPos == -1) ? strTemp : strTemp.Left(iPos);
		int iSubPos = 0;
		{
			iSubPos = strLeft.Find(_T(" "));
			strSimpleWord = strLeft.Left(iSubPos).GetAt(0);
			strLeft = strLeft.Mid(iSubPos+1,strLeft.GetLength());
			iSubPos = strLeft.Find(_T(" "));
			*strPronunciation = strLeft.Left(iSubPos);
		}
		m_PronunciationLib.insert( std::make_pair(strSimpleWord[0],strPronunciation) );
		if (iPos == -1)
		{
			return ;
		}
		strTemp = strTemp.Mid(iPos+1,strTemp.GetLength());
	}
}